package com.newcapec.servlet;

import com.newcapec.bean.ColumnEntity;
import com.newcapec.bean.NewsEntity;
import com.newcapec.bean.UserEntity;
import com.newcapec.service.ColumnService;
import com.newcapec.service.NewsService;
import com.newcapec.service.impl.ColumnServiceImpl;
import com.newcapec.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description:显示前端主界面的servlet类
 * @Author:yangshupeng
 * @Date: 2022/3/21 22:11
 * @Version:1.0
 */
@WebServlet("/main.html")
public class ShowMainHtmlServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的栏目
        ColumnService columnService = new ColumnServiceImpl();
        List<ColumnEntity> columnList = columnService.queryAll();
        //获取默认的栏目
        ColumnEntity columnEntity = columnList.get(0);
        int column_id = columnEntity.getId();
        //根据栏目id查询指定的新闻信息
        String columnId = req.getParameter("columnId");
        if (columnId != null){
            column_id = Integer.parseInt(columnId);
        }
        NewsService newsService = new NewsServiceImpl();
        //通过chnl_code查相同栏目的新闻
        List<NewsEntity> newsList = newsService.queryByColumnId(column_id);

        req.setAttribute("columnList",columnList);
        req.setAttribute("newsList",newsList);
        //请求转发
        req.getRequestDispatcher("web/main.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
